package com.example.myapplication.viewmodel;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import com.example.myapplication.MyConstant;

import java.util.ArrayList;
import java.util.Random;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.SavedStateHandle;

public class MyViewModel extends AndroidViewModel {
    private int level = 100;

    private SavedStateHandle handle;
    public boolean win_flag;
    private  Random random;
    public MyViewModel(@NonNull Application application, SavedStateHandle handle) {
        super(application);
        this.handle = handle;
        random = new Random();
        if (!handle.contains(MyConstant.KEY_HIGHEST_SCORE)){
            SharedPreferences sp = getApplication().getSharedPreferences(MyConstant.SAVE_SP_DATE_NAME, Context.MODE_PRIVATE);
            handle.set(MyConstant.KEY_HIGHEST_SCORE,sp.getInt(MyConstant.KEY_HIGHEST_SCORE,0));
            handle.set(MyConstant.KEY_ANSWER,0);
            handle.set(MyConstant.KEY_LEFT_NUM,0);
            handle.set(MyConstant.KEY_RIGHT_NUM,0);
            handle.set(MyConstant.KEY_CURRENT_SCORE,0);
            handle.set(MyConstant.KEY_OPERATOR,"+");

        }
    }

    public MutableLiveData<Integer> getLeftNum(){
        MutableLiveData<Object> liveData = handle.getLiveData(MyConstant.KEY_LEFT_NUM);
        Object value = liveData.getValue();
        return handle.getLiveData(MyConstant.KEY_LEFT_NUM,0);
    }
    public MutableLiveData<Integer> getRightNum(){
        return handle.getLiveData(MyConstant.KEY_RIGHT_NUM,0);
    }
    public MutableLiveData<Integer> getHighestScore(){
        return handle.getLiveData(MyConstant.KEY_HIGHEST_SCORE,0);
    }
    public MutableLiveData<String> getOperator(){
        return handle.getLiveData(MyConstant.KEY_OPERATOR);
    }
    public MutableLiveData<Integer>getAnwser(){
        return handle.getLiveData(MyConstant.KEY_ANSWER);
    }
    public MutableLiveData<Integer>getCurrentScore(){
        return handle.getLiveData(MyConstant.KEY_CURRENT_SCORE,0);
    }

    public MutableLiveData<ArrayList<String>> getOperatorList(){
        return handle.getLiveData(MyConstant.OPERATOR_LIST,new ArrayList<String>());
    }

    public void generator(){
        ArrayList<String> listOperator = getOperatorList().getValue();
        if (listOperator.size() == 0){
            //如果没有设置，默认加减法
            generatorAdd_Subtraction();
        }else if (listOperator.size() == 1){
            String operator = listOperator.get(0);
            calculator(operator);
        }else{
            int x = random.nextInt(level)+1;
            if (x % listOperator.size() == 0){
                calculator(listOperator.get(0));
            }else if (x % listOperator.size() == 1){
                calculator(listOperator.get(1));
            }else {
                int s = listOperator.size()- 1;
                int c = random.nextInt(s);
                calculator(listOperator.get(c));
            }
        }

    }

    public void generatorAdd_Subtraction(){
//        Random random = new Random();
        int x,y;
        x = random.nextInt(level)+1;
        y = random.nextInt(level)+1;
        if (x % 2 == 0){//为了让 + - 号出现概率各为一半
            getOperator().setValue("+");
            getLeftNum().setValue(x);
            getRightNum().setValue(y);
            getAnwser().setValue(x+y);
        }else {
            getOperator().setValue("-");
            //为了不出现负数
            if (x>y){
                getLeftNum().setValue(x);
                getRightNum().setValue(y);
                getAnwser().setValue(x-y);
            }else {
                getLeftNum().setValue(y);
                getRightNum().setValue(x);
                getAnwser().setValue(y-x);
            }
        }
    }

    private void calculator(String operator){
        if ("+".equals(operator)){
            //加法
            add();
        }else if ("-".equals(operator)){
            subtraction();
        } else if ("x".equals(operator)) {
            multiplication();
        }else if ("÷".equals(operator)){
            division();
        }
    }

    //加
    private void add(){

        int x,y;
        x = random.nextInt(level)+1;
        y = random.nextInt(level)+1;
        getOperator().setValue("+");
        getLeftNum().setValue(x);
        getRightNum().setValue(y);
        getAnwser().setValue(x+y);
    }
    //减
    private void subtraction(){
        getOperator().setValue("-");
        //为了不出现负数
        int x,y;
        x = random.nextInt(level)+1;
        y = random.nextInt(level)+1;
        if (x>y){
            getLeftNum().setValue(x);
            getRightNum().setValue(y);
            getAnwser().setValue(x-y);
        }else {
            getLeftNum().setValue(y);
            getRightNum().setValue(x);
            getAnwser().setValue(y-x);
        }
    }
    //乘
    private void multiplication(){
        int x,y;
        x = random.nextInt(10)+1;
        y = random.nextInt(10)+1;
        getOperator().setValue("x");
        getLeftNum().setValue(x);
        getRightNum().setValue(y);
        getAnwser().setValue(x * y);
    }
    //除
    private void division(){
        int x,y;
        x = random.nextInt(level)+1;
        y = random.nextInt(8)+1;
        getOperator().setValue("÷");
        getLeftNum().setValue((x * y));
        getRightNum().setValue(y);
        getAnwser().setValue(x);
    }

    //保存最高纪录
    public void saveHighestScore(){
        SharedPreferences sp = getApplication().getSharedPreferences(MyConstant.SAVE_SP_DATE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putInt(MyConstant.KEY_HIGHEST_SCORE,getHighestScore().getValue());
        edit.apply();
    }
    //回答正确
    public void anwserCorrect(){
        getCurrentScore().setValue(getCurrentScore().getValue()+1);
        //如果创造了纪录
        if (getCurrentScore().getValue() > getHighestScore().getValue()){
            getHighestScore().setValue(getCurrentScore().getValue());
            win_flag = true;
        }
        //回答正确出下一题
        generator();
    }



}
